<template>
    <uni-popup ref="popup" :mask-click="false" type="bottom" background-color="#fff">
        <view class="login-container">
            <!--提示-->
            <view class="tips">
                <text>登录后享受更多权益</text>
                <text>99%+的用户选择微信头像和昵称，便于订单发货和售后服务</text>
            </view>
            <!--头像-->
            <view class="avatar">
                <button plain class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
                    <image :src="defaultAvatarUrl"></image>
                </button>
            </view>
            <!--昵称-->
            <view class="nickname-input">
                昵称<input type="nickname" class="weui-input" :placeholder="placeholderValue"
                           placeholder-class="center"
                           @focus="clearPlaceholder" @blur="recoverPlaceholder" @change="getNickname"/>
            </view>
            <!--登录-->
            <view class="login-btn">
                <button type="primary" style="background-color: #ff93a7" open-type="getUserInfo"
                        @getuserinfo="getUserInfo">登录
                </button>
            </view>
        </view>
    </uni-popup>
</template>

<script>
    import {mapState,mapMutations} from "vuex";

    export default {
        computed: {
            ...mapState('user',['redirectInfo'])
        },
        data() {
            return {
                defaultAvatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
                placeholderValue: '请输入昵称',
                userInfo: {
                    avatarUrl: "",
                    city: "",
                    country: "",
                    gender: 0,
                    language: "",
                    nickname: "",
                    province: "",
                },
                token: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
            }
        },
        onLoad() {
            this.onChooseAvatar()
        },
        onReady() {
            this.showModal()
        },
        methods: {
            ...mapMutations('user', ['updateUserInfoObj', 'updateToken','updateRedirectInfo']),
            onChooseAvatar(e) {
                this.defaultAvatarUrl = e.detail.avatarUrl
                this.userInfo.avatarUrl = e.detail.avatarUrl
            },
            clearPlaceholder() {
                this.placeholderValue = ''
            },
            recoverPlaceholder() {
                if (this.placeholderValue === '') return this.placeholderValue = '请输入昵称'
            },
            getNickname(e) {
                if (e.detail.value === '') return false
                this.userInfo.nickname = e.detail.value
            },
            getUserInfo(e) {
                if (this.userInfo.avatarUrl === '') return uni.$showMsg('请选择头像')
                if (this.userInfo.nickname === '') return uni.$showMsg('请输入昵称')
                this.updateUserInfoObj(this.userInfo)
                this.getToken(e.detail)
                this.$refs.popup.close()
                this.navigateBack()
            },
            async getToken(info) {
                const [err, res] = await uni.login().catch(err => err)
                if (err || res.errMsg !== "login:ok") return uni.$showMsg('登录失败')
                /*const query = {
                    code: res.code,
                    encryptedData: info.encryptedData,
                    iv: info.iv,
                    rawData: info.rawData,
                    signature: info.signature,
                    token:'11111111111122222222222222'
                }*/
                // const res1 = await uni.$http.post('/api/public/v1/users/wxlogin',query)
                this.updateToken(this.token)
            },
            showModal() {
                this.$refs.popup.open()
            },
            navigateBack(){
                if (this.redirectInfo && this.redirectInfo.openType === 'switchTab'){
                    uni.switchTab({
                        url:this.redirectInfo.from,
                        complete: () => {
                            this.updateRedirectInfo(null)
                        }
                    })
                }
            }
        },
    }
</script>

<style lang="scss">
    .login-container {
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
        align-items: center;
        width: 100%;
        height: 400px;

        .avatar {
            width: 100%;
            height: 135px;
            border-bottom: 1px solid #efefef;
            display: flex;
            justify-content: center;
            margin-top: -30px;

            button {
                border: none;
                padding: 0;
                margin: 0;
                display: flex;
                align-items: center;

                image {
                    width: 120px;
                    height: 120px;
                    border-radius: 100%;
                    border: 1px solid #efefef;
                }
            }
        }

        .nickname-input {
            width: 100%;
            height: 60px;
            border-bottom: 1px solid #efefef;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: -30px;

            input {
                width: 100px;
                height: 20px;
                text-align: center;
                margin-right: 25px;

                .center {
                    //text-align: center;
                }
            }
        }

        .login-btn {
            width: 95%;
            //margin-top: 30px;
        }

        .tips {
            width: 100%;
            height: 60px;
            color: #bebdbd;
            font-size: 12px;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: -25px;

            text:first-child {
                color: black;
                font-size: 16px;
                font-weight: bold;
                padding: 5px 0;
            }
        }
    }
</style>
